package;
import fl.controls.Button;
import fl.controls.List;
import fl.controls.TextArea;
import fl.controls.TextInput;
import fl.data.DataProvider;

import flash.Lib;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.net.NetConnection;
import flash.net.Responder;

class Main extends MovieClip
{
	static var app:String='/connect4';
	static public var path:String;
	var nc:NetConnection;
	var uname:String;
	var login : Login;
	var game : Game;
	var lobby : Lobby;
	public var console:Console;
	public var connection:NetConnection;
	public var userName:String;

	public static function main()
	{
		Lib.current.stage.scaleMode=StageScaleMode.NO_SCALE;
		Lib.current.stage.align=StageAlign.TOP_LEFT;
		Lib.current.addChild(new Main());
	}
	public function new()
	{
		super();
		console = new Console();
		addChild(console);
		console.log("starting...");
		addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
	}
	function onAddedToStage(event:Event):Void
	{
		removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		path = "rtmp://"+getUri();
		init();
	}
	function init()
	{
		connection = new NetConnection();
		connection.objectEncoding = 0;
		login = new Login(connection, console);
		login.addEventListener("login", onLogin);
		addChild(login);
	}
	function onLogin(event:Event):Void
	{
		//connection.client=this;
		connection.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
		userName = event.currentTarget.getUserName();
		removeChild(login);
		lobby = new Lobby(connection, console, userName);
		lobby.addEventListener("newGame", onGetNewGame);
		addChild(lobby);
		
		var obj:Dynamic = flash.Lib.current.root.loaderInfo.parameters;
		if(obj.id!=0 && !Math.isNaN(obj.id))
		{
			console.log(obj.id);
			game = new Game(obj.id, userName, console, lobby);
			addChild(game);
		}
	}
	function onGetNewGame(event:Event)
	{
		console.log(event.currentTarget.getGameId());
		game = new Game(event.currentTarget.getGameId(), userName, console, lobby);
		addChild(game);
	}
	function onNetStatus(event:NetStatusEvent):Void
	{
		console.log(event);
		if(event.info.code =='NetConnection.Connect.Closed')
		{
			connection.removeEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
			login = new Login(connection, console);
			login.addEventListener("login", onLogin);
			addChild(login);
		}
	}
	function getUri():String
	{
		var parameters:Dynamic = flash.Lib.current.loaderInfo.parameters;
		//trace(parameters);
		var u = Reflect.field(parameters, "uri");
		if(u!=null)
			return u;
		var h = Reflect.field(parameters, "host");
		var p = Reflect.field(parameters, "port");
		var a = Reflect.field(parameters, "app");
		return (h==null?"localhost":h) + (p==null?":1935":":"+p) + (a==null?app:a);
	}
}